<?php declare(strict_types=1);

namespace App\Api\V2\Controller\Store;

use App\Api\Controllers\BaseController;
use App\Exceptions\BizException;
use App\Models\CommonConfigs;
use App\Models\QuotaBank;
use App\Models\QuotaConfig;
use App\Services\Mch\MchService;
use App\Services\Pay\GuoTonXinYi\Pay;
use App\Services\PrintSqlService;
use App\Services\Quota\GtxyQuotaService;
use App\Services\Store\StoreMchService;
use App\Traits\ResponseTrait;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use TencentCloud\Cdn\V20180606\Models\Quota;

class QuotaController extends BaseController
{
    use ResponseTrait;

    public function getQuota(Request $request): string
    {
        try {
            $store_id = $request->input("store_id", "");
            $company = $request->input("company", "");
            $mchNo = StoreMchService::getMchNo($store_id, $company);
            if (empty($mchNo)) {
                throw new BizException("商户号不存在");
            }
            $quotaConfig = QuotaConfig::query()->where("mch_id", $mchNo)->where("company", $company)->first();
            $result = [
                "quota_rate" => 0,
                "bank_name" => ""
            ];
            if (!empty($quotaConfig)) {
                $result = [
                    "quota_rate" => $quotaConfig->quota_rate,
                    "bank_name" => $quotaConfig->bank_name
                ];
            }
            return $this->successResponse("ok", ["data" => $result]);
        } catch (\Exception $e) {
            return $this->errorResponse($e->getMessage());
        }
    }

    public function setQuota(Request $request): string
    {
        try {
            $store_id = $request->post("store_id", "");
            $company = $request->post("company", "");
            $quota_rate = $request->post("quota_rate", 0);
            $bank_name = $request->post("bank_name", "");
            if (empty($store_id)) {
                throw new BizException("店铺ID不能为空");
            }
            $mchNo = StoreMchService::getMchNo((string)$store_id, $company);
            if (empty($mchNo)) {
                throw new BizException("商户号不存在");
            }

            $quotaConfig = QuotaConfig::query()->where("mch_id", $mchNo)->where("company", $company)->first();
            if (!empty($quotaConfig)) {
                $quotaConfig->quota_rate = $quota_rate;
                $quotaConfig->bank_name = $bank_name;
                $result = $quotaConfig->save();
            } else {
                $result = QuotaConfig::query()->create([
                    "mch_id" => $mchNo,
                    "company" => $company,
                    "quota_rate" => $quota_rate,
                    "bank_name" => $bank_name,
                ]);
            }

            if (empty($result)) {
                throw new BizException("更新失败");
            }
            return $this->successResponse("ok", ["data" => true]);
        } catch (\Exception $e) {
            return $this->errorResponse($e->getMessage());
        }
    }

    public function quotaBank(Request $request): string
    {
        $bank = QuotaBank::query()->where("company", $request->input("company"))->get()->toArray();
        return $this->successResponse("ok", ["data" => $bank]);
    }

}